package com.navinfo.platform.common.utils;

import java.math.BigDecimal;

public class DataUtil {

    public static  <T> T getValue(Object o, Object defaultValue){
        if(o == null){
            return (T)defaultValue;
        }
        return (T)o;
    }

    /**
     * 计算百公里次数
     * @param sumTimes
     * @param sumMileage  单位为米
     * @return
     */
    public static  int hundredKmTimes(int sumTimes, int sumMileage){
        if(sumTimes==0 || sumMileage/1000 == 0){
            return 0;
        }
        return BigDecimal.valueOf(sumTimes*100).divide(BigDecimal.valueOf(sumMileage/1000),4 , BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(10000)).intValue();
    }

    public static Long hundredKmTimes(Long sumTimes, Long sumMileage){
        if(sumTimes == null || sumTimes==0 || sumMileage == null || sumMileage/1000 == 0){
            return null;
        }
        return BigDecimal.valueOf(sumTimes*100).divide(BigDecimal.valueOf(sumMileage/1000),4 , BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(10000)).longValue();
    }

    /**
     * 计算时长占比
     * @param duration
     * @param sumDuration
     * @return
     */
    public static  int durationProportion(int duration, int sumDuration){
        if(duration==0 || sumDuration==0){
            return 0;
        }
        return BigDecimal.valueOf(duration).divide(BigDecimal.valueOf(sumDuration),4 , BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(10000)).intValue();
    }

    public static Long durationProportion(Long duration, Long sumDuration){
        if(duration == null || sumDuration == null || duration == 0 || sumDuration == 0){
            return null;
        }
        return BigDecimal.valueOf(duration).divide(BigDecimal.valueOf(sumDuration),4 , BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(10000)).longValue();
    }

    /**
     * 计算里程占比
     * @param mileage
     * @param sumMileage
     * @return
     */
    public static  int mileageProportion(int mileage, int sumMileage){
        return durationProportion(mileage, sumMileage);
    }

    public static Long mileageProportion(Long mileage, Long sumMileage){
        return durationProportion(mileage, sumMileage);
    }

    public static int remainTwoBit(int n, int m){
        return BigDecimal.valueOf(n).divide(BigDecimal.valueOf(m),2 , BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(100)).intValue();
    }

    public static Long remainTwoBit(Long n, Long m){
        if(n == null || n == 0 || m == null || m == 0){
            return null;
        }
        return BigDecimal.valueOf(n).divide(BigDecimal.valueOf(m),2 , BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(100)).longValue();
    }
    public static Long remainTwoBit(double n, Long m){
        if( n == 0 || m == null || m == 0){
            return null;
        }
        return BigDecimal.valueOf(n).divide(BigDecimal.valueOf(m),2 , BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(100)).longValue();
    }
}
